/**
 * @file lv_port_indev.c
 *
 */

 /*Copy this file as "lv_port_indev.c" and set this value to "1" to enable content*/
#if 1

/*********************
 *      INCLUDES
 *********************/
#include "lv_port.h"
#include "lvgl.h"
#include "drv_touch.h"
/*********************
 *      DEFINES
 *********************/


/**********************
 *      TYPEDEFS
 **********************/


/**********************
 *  STATIC PROTOTYPES
 **********************/


/**********************
 *  STATIC VARIABLES
 **********************/


/**********************
 *   STATIC FUNCTIONS
 **********************/

/* Will be called by the library to read the touchpad */
static bool touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
	static uint16_t last_x = 0;
	static uint16_t last_y = 0;
	// if(tp_dev.sta&TP_PRES_DOWN)//触摸按下了
	// {
	// 	last_x = tp_dev.x[0];
	// 	last_y = tp_dev.y[0];
	// 	data->point.x = last_x;
	// 	data->point.y = last_y;
	// 	data->state = LV_INDEV_STATE_PR;
	// }else{
	// 	data->point.x = last_x;
	// 	data->point.y = last_y;
	// 	data->state = LV_INDEV_STATE_REL;
	// }

	static _drv_touch_point_t tp_t;

	tp_t.point[0].sta = TOUCH_STA_NONE;

	drv_touch_scan(&tp_t);
	if (tp_t.point[0].sta == TOUCH_STA_DOWN)
	{
		data->point.x = tp_t.point[0].x;
		data->point.y = tp_t.point[0].y;
		data->state = LV_INDEV_STATE_PR;
		last_x = data->point.x;
		last_y = data->point.y;
	}
	else
	{
		data->point.x = last_x;
		data->point.y = last_y;
		data->state = LV_INDEV_STATE_REL;
	}

    /*Return `false` because we are not buffering and no more data to read*/
    return false;
}


/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

void lv_port_indev_init(void)
{
    lv_indev_drv_t indev_drv;

    /*Register a touchpad input device*/
    lv_indev_drv_init(&indev_drv);
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = touchpad_read;
    lv_indev_drv_register(&indev_drv);


}






#else /* Enable this file at the top */

/* This dummy typedef exists purely to silence -Wpedantic. */
typedef int keep_pedantic_happy;
#endif
